/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package todotask.model;

import java.awt.Component;
import java.util.EventObject;
import java.util.LinkedList;
import javax.swing.JTable;
import javax.swing.event.CellEditorListener;
import javax.swing.table.TableCellEditor;
import todotask.entities.Task;
import todotask.view.TaskInfo;

/**
 *
 * @author dakota
 */
public class TaskCellEditor extends TaskInfo implements TableCellEditor {

    /** Lista de suscriptores */
    private LinkedList suscriptores = new LinkedList();

    public TaskCellEditor() {
        //
    }

    /**  Sets an initial <code>value</code> for the editor.  This will cause
     *  the editor to <code>stopEditing</code> and lose any partially
     *  edited value if the editor is editing when this method is called. <p>
     *
     *  Returns the component that should be added to the client's
     *  <code>Component</code> hierarchy.  Once installed in the client's
     *  hierarchy this component will then be able to draw and receive
     *  user input.
     *
     * @param	table		the <code>JTable</code> that is asking the
     * 				editor to edit; can be <code>null</code>
     * @param	value		the value of the cell to be edited; it is
     * 				up to the specific editor to interpret
     * 				and draw the value.  For example, if value is
     * 				the string "true", it could be rendered as a
     * 				string or it could be rendered as a check
     * 				box that is checked.  <code>null</code>
     * 				is a valid value
     * @param	isSelected	true if the cell is to be rendered with
     * 				highlighting
     * @param	row     	the row of the cell being edited
     * @param	column  	the column of the cell being edited
     * @return	the component for editing
     *
     */
    @Override
    public Component getTableCellEditorComponent(JTable jtable, Object o, boolean bln, int i, int i1) {
        // Devolvemos el JComboBox del que heredamos.
        setTask((Task) o);
        getJPanel().setImageBackground(true);
        getJPanel().setBackground(((Task) o).getCategory().getColor());
        return this;
    }

    /** Returns the value contained in the editor.
     * @return the value contained in the editor
     *
     */
    @Override
    public Object getCellEditorValue() {
        Task t = getT();
        setT(t);
        getJPanel().setImageBackground(true);
        getJPanel().setBackground(t.getCategory().getColor());
        return this;
//        throw new UnsupportedOperationException("Not supported yet.");
        // Se obtiene la opción del combo box elegida y se devuelve un
        // Integer adecuado.
//        switch (this.getSelectedIndex()) {
//            // joven
//            case 0:
//                return new Integer(15);
//            // hombre
//            case 1:
//                return new Integer(33);
//            // viejo
//            case 2:
//                return new Integer(99);
//        }
//
//        // Devolvemos null si no nos gusta la elección del menú.
//        return null;
    }

    /** Asks the editor if it can start editing using <code>anEvent</code>.
     * <code>anEvent</code> is in the invoking component coordinate system.
     * The editor can not assume the Component returned by
     * <code>getCellEditorComponent</code> is installed.  This method
     * is intended for the use of client to avoid the cost of setting up
     * and installing the editor component if editing is not possible.
     * If editing can be started this method returns true.
     *
     * @param	anEvent		the event the editor should use to consider
     * 				whether to begin editing or not
     * @return	true if editing can be started
     * @see #shouldSelectCell
     *
     */
    @Override
    public boolean isCellEditable(EventObject eo) {
        // La celda es editable ante cualquier evento.
        return true;
    }

    /** Returns true if the editing cell should be selected, false otherwise.
     * Typically, the return value is true, because is most cases the editing
     * cell should be selected.  However, it is useful to return false to
     * keep the selection from changing for some types of edits.
     * eg. A table that contains a column of check boxes, the user might
     * want to be able to change those checkboxes without altering the
     * selection.  (See Netscape Communicator for just such an example)
     * Of course, it is up to the client of the editor to use the return
     * value, but it doesn't need to if it doesn't want to.
     *
     * @param	anEvent		the event the editor should use to start
     * 				editing
     * @return	true if the editor would like the editing cell to be selected;
     *    otherwise returns false
     * @see #isCellEditable
     *
     */
    @Override
    public boolean shouldSelectCell(EventObject eo) {
        // Indica si al editar la celda, debemos seleccionar la fila que la
        // contiene.
        return true;
    }

    /** Tells the editor to stop editing and accept any partially edited
     * value as the value of the editor.  The editor returns false if
     * editing was not stopped; this is useful for editors that validate
     * and can not accept invalid entries.
     *
     * @return	true if editing was stopped; false otherwise
     *
     */
    @Override
    public boolean stopCellEditing() {
        // Indica si se puede detener la edición.
        return true;
    }

    @Override
    public void cancelCellEditing() {
        // No hay que hacer nada especial.
    }

    /** Adds a listener to the list that's notified when the editor
     * stops, or cancels editing.
     *
     * @param	l		the CellEditorListener
     *
     */
    @Override
    public void addCellEditorListener(CellEditorListener cl) {
        // Se añade el suscriptor a la lista.
        suscriptores.add(cl);
    }

    /** Removes a listener from the list that's notified
     *
     * @param	l		the CellEditorListener
     *
     */
    @Override
    public void removeCellEditorListener(CellEditorListener cl) {
        // Se elimina el suscriptor.
        suscriptores.remove(cl);
    }
}
